package com.android.lib.base.ext

import android.content.Context
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import net.lucode.hackware.magicindicator.MagicIndicator
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView

fun ViewPager2.initFragment(
   fragments: MutableList<Fragment>
): ViewPager2 {
    //设置适配器
    adapter = object : FragmentStateAdapter(context as AppCompatActivity) {
        override fun createFragment(position: Int) = fragments[position]
        override fun getItemCount() = fragments.size
    }
    return this
}

fun ViewPager2.bindMagicIndicator(magicIndicator: MagicIndicator, titleList: ArrayList<String>):CommonNavigator {
    val commonNavigator = CommonNavigator(context)
    commonNavigator.isAdjustMode = true
    commonNavigator.adapter = object : CommonNavigatorAdapter() {
        override fun getCount(): Int {
            return titleList.size
        }

        override fun getTitleView(context: Context, index: Int): IPagerTitleView {
            val clipPagerTitleView = SimplePagerTitleView(context)
            clipPagerTitleView.normalColor = Color.parseColor("#0000ff")
            clipPagerTitleView.selectedColor = Color.parseColor("#ff0000")
            clipPagerTitleView.text = titleList[index]
            clipPagerTitleView.setOnClickListener {
                currentItem = index
            }
            return clipPagerTitleView
        }

        override fun getIndicator(context: Context): IPagerIndicator? {
            val indicator = LinePagerIndicator(context)
            indicator.mode = LinePagerIndicator.MODE_EXACTLY
            indicator.yOffset = UIUtil.dip2px(context, 3.0).toFloat()
            indicator.setColors(Color.parseColor("#ff0000"))
            return indicator
        }
    }
    magicIndicator.navigator = commonNavigator
    registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels)
        }

        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            magicIndicator.onPageSelected(position)
        }

        override fun onPageScrollStateChanged(state: Int) {
            super.onPageScrollStateChanged(state)
            magicIndicator.onPageScrollStateChanged(state)
        }
    })
    return  commonNavigator
}

